import React from 'react';
import { connect } from 'dva';
import PageHeaderLayout from 'components/Layout/PageHeaderLayout';
import UpdateForm from './UpdateForm';
import { Page } from 'components';
import { LwjDialog } from 'components';
import { Modal } from 'antd';
import './index.less'

const UpdateOrder = ({ location, dispatch, updateOrder, loading }) => {

  let { isUpdate, salesmen, distributors, currentOrderDetail, listState, isFormDirty,isLoading } = updateOrder;

  const setLoading = (option) => {
    dispatch({
      type:'updateOrder/setIsLoading',
      payload:option
    })
  }
  const breadcrumbList = [
    {
      title: '订单管理',
      href: `/orderList/${listState}`
    }, {
      title: listState === "ALL" ? "全部订单" : "我的订单",
      href: `/orderList/${listState}`
    }, {
      title: '添加意向单'
    }
  ];

  const updateFormProps = {
    isUpdate,
    salesmen,
    distributors,
    currentOrderDetail,
    dispatch,
    isFormDirty,
    isLoading,
    setLoading,
    saveOrder(values) {
      if (isUpdate) {
        dispatch({
          type: 'updateOrder/updateSalesOrder',
          payload: {
            salesOrder: values
          }
        });
      } else {
        dispatch({
          type: 'updateOrder/createSalesOrder',
          payload: {
            salesOrder: values
          }
        });
      }
    },
    submitOrder() {
      if (isFormDirty) {
        Modal.info({ title: '请先保存当前修改，再进行提交' });
        return;
      }
      setLoading(true)
      LwjDialog.confirm({
        title: '请确认意向单信息无误',
        content: '确认下单后，意向单信息将无法修改',
        okText: '确认',
        cancelText: '取消',
        onOk() {
          dispatch({ type: 'updateOrder/submitSalesOrder', payload: {} });
        },
        onCancel() {
          setLoading(false)
        }
      })
    }
  };

  return (
    <PageHeaderLayout
      title={isUpdate
        ? "修改意向单"
        : "添加意向单"}
      breadcrumbList={breadcrumbList}>
      <Page inner>
        {(!isUpdate || !_.isEmpty(currentOrderDetail)) && < UpdateForm {...updateFormProps} />}
      </Page>
    </PageHeaderLayout>
  )
};

export default connect(({ updateOrder, loading }) => ({ updateOrder, loading }))(UpdateOrder)
